package com.cheyun.netsalev3.view.aftersale

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.ActivityMaintenanceDetailsBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.aftersale.MaintenanceDetailsActivityViewModel
import com.jeremyliao.liveeventbus.LiveEventBus

class MaintenanceDetailsActivity : BaseActivity<ActivityMaintenanceDetailsBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_maintenance_details
    }


    var oid = 0
    override fun initViewModel(): ViewModel? {
        if(intent.hasExtra("id")) {
            oid = intent.getIntExtra("id", 0)
        }

        var factory = InjectorUtils.getMaintenanceDetailsActivityViewModelFactory(oid)
        return ViewModelProviders.of(this, factory).get(MaintenanceDetailsActivityViewModel::class.java)
    }

    override fun initData() {
        binding.viewModel?.phoneMsg?.observe(this, Observer { it ->
            if(it.indexOf("*") !=-1){
                binding.messageBut.visibility = View.GONE
            }
        })
        binding.viewModel?.state?.observe(this, Observer { it ->
            if(it == 0 || it== 1){
                binding.linearLayout.visibility = View.VISIBLE
                binding.btnEdit.visibility = View.VISIBLE
            }
        })
    }

    override fun initView() {
        binding.messageBut.setOnClickListener {
            binding.phoneText.text.apply {
                FunctionUtils.sendSMSTo(this@MaintenanceDetailsActivity, binding.phoneText.text.toString(), "")
            }
        }
        LiveEventBus.get()
                .with("aftersale_maintenance_detail_refresh", String::class.java)
                .observe(this, Observer {
                    binding.viewModel?.initData()
                })
    }


}
